//导包

var myUtils = require("../../utils/myUtils.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    long: 0,
    lat: 0,
    controls: [],
    markers: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getLocation({
      success: function(res) {
        var longitude = res.longitude 
        var latitude = res.latitude
        // console.log(res)
        that.setData({
          long: longitude,
          lat: latitude
        })
      },
    })

    wx.getSystemInfo({
      success: function(res) {
        var windowHeight = res.windowHeight
        var windowWidth = res.windowWidth
        that.setData({
          controls: [
            //开锁按钮
            {
              id: 0,
              iconPath: '/images/lock.png',
              position: {
                width: 100,
                height: 45,
                left: windowWidth / 2 - 50,
                top: windowHeight - 60
              },
              clickable: true
            },
            //充值按钮
            {
              id: 1,
              iconPath: '/images/chongzhi.png',
              position: {
                width: 35,
                height: 35,
                left: windowWidth - 45,
                top: windowHeight - 100
              },
              clickable: true
            },
            //报修
            {
              id: 2,
              iconPath: '/images/baoxiu.png',
              position: {
                width: 35,
                height: 35,
                left: windowWidth - 45,
                top: windowHeight - 60
              },
              clickable: true
            },
            //定位按钮
            {
              id: 3,
              iconPath: '/images/locate.png',
              position: {
                width: 35,
                height: 35,
                left: 10,
                top: windowHeight - 60
              },
              clickable: true
            },

            //位置图标
            {
              id: 4,
              iconPath: '/images/cur.png',
              position: {
                width: 35,
                height: 35,
                left: 10,
                top: windowHeight / 2 - 35,
                left: windowWidth / 2 - 18
              },
              clickable: true
            },

            //添加车辆
            {
              id: 5,
              iconPath: '/images/Add.png',
              position: {
                width: 35,
                height: 35,
              },
              clickable: true
            },
          ]
        })
      },
    })
  },

  //控件被点击事件
  controlTap: function(e) {
    var that = this
    var cId = e.controlId;
    switch (cId) {
      //点击扫码按钮触发事件
      case 0:
        {
          var status = myUtils.get("status")

          //根据用户状态跳转到对应的页面         
          //如果状态为0，则跳转到注册页面
          if(status == 0){
            wx.navigateTo({
              url: '../register/register',
            })
          }else if(status == 1){
            wx.navigateTo({
              url: '../deposite/deposite',
            })
          }else if(status == 2){
            wx.navigateTo({
              url: '../identify/identify',
            })
          }
          break;
        }
      case 3:
        {
          this.mapCtx.moveToLocation()
          break;
        }

      case 5:
        {
          //获取当前已有的车辆
          var bikes = that.data.markers
          //获取移动后的位置的中心点
          this.mapCtx.getCenterLocation({
            success: function(res) {
              var relong = res.longitude
              var relat = res.latitude
              //往里面添加单车
              // bikes.push({
              //   iconPath: '/images/bike@red.png',
              //   width: 30,
              //   height: 30,
              //   longitude: relong,
              //   latitude: relat
              // })
              // that.setData({
              //   markers: bikes
              // })
              //将添加的单车数据发送到后台
              wx.request({
                url: 'http://localhost:8080/bike/add',
                data: {
                  longitude: relong,
                  latitude: relat,
                  status:0
                },
                method:'POST',
                success:function(res){
                  //查找单车，然后将单车显示在页面上
                  findBikes(relong,relat,that)
                }
              })
            }
          })
          break;
        }
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //创建map上下文
    this.mapCtx = wx.createMapContext('myMap')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})

function findBikes(longitude,latitude,that){
  wx.request({
    url: 'http://localhost:8080/bike/findNear',
    method:"GET",
    data:{
      longitude:longitude,
      latitude:latitude 
    },
    success:function(res){
      console.log(res)
    }
  })
}